﻿@{
    ViewBag.Title = "添加电梯信息";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Head{
    <script src="~/Scripts/http.js"></script>
    @Styles.Render("~/Content/kendo")
    @Scripts.Render("~/bundles/kendo")
}
@using AFT.Parking.Entity;
@model ParkingLift
<div class="content-box">
    <!-- Start Content Box -->
    <div class="content-box-header">
        <h3>添加电梯信息</h3>
        <div class="clear"></div>
    </div>
    <!-- End .content-box-header -->
    <p>
        <div id="tab2" class="k-edit-form-container">
            <div class="k-edit-label">电梯名称：</div>
            <div class="k-edit-field">
                <input type="text" id="name" class="k-input k-textbox" data-bind="value:name" />
            </div>
            <div class="k-edit-label">关联电梯：</div>
            <div class="k-edit-field">
                <select class="k-widget k-multiselect k-header" id="selLifts" data-bind="value:crossTo" multiple="multiple"></select>
            </div>
            <div id="msg" class="k-edit-field"></div>
            <div class="k-edit-buttons k-state-default">
                <input class="button" type="button" id="btnSubmit" value=" 保 存 " />
                <input class="button" type="button" id="btnBack" value=" 返 回 " /> <br />
                <span class="input-notification">@ViewBag.Message</span>
                <input type="hidden" id="hd_backUrl" value="/parkingAreas/lifts?parkingId=@Model.ParkingId">
            </div>
        </div>
    </p>
</div>

<script>
    $().ready(function () {
        var multiselect = $("#selLifts").kendoMultiSelect({
            placeholder: "请选择...",
            dataTextField: "Name",
            dataValueField: "Id",
            dataSource: {
                type: "json",
                serverFiltering: true,
                transport: {
                    read: {
                        url: "/ParkingAreas/GetIsLifts/" + "@Model.ParkingId",
                    }
                }
            }
        });
        var liftData = kendo.observable({
            name: "",
            crossTo: ""
        });
        kendo.bind($("#tab2"), liftData);
        $("#btnSubmit").click(function () {
            var json = {};
            json.name = liftData.name;
            json.crossTo = find(liftData.toJSON().crossTo);
            json.parkingId = "@Model.ParkingId";
            http.post("/ParkingAreas/AddLift", json).then(function (result) {
                if (result) {
                    if (result === "保存成功。") {
                        $("#msg").html("<b style='color: green'> " + result + "</b>");
                        liftData.name = "";
                        $("#name").val("");
                    } else {
                        $("#msg").html("<b style='color: red'> " + result + "</b>");
                    }
                }
            });
        });
    });



    function find(item) {
        var result = [];
        if (item) {
            $.each(item, function (index, init) {
                if (init) {
                    result.push(init.Id);
                }
            });
        }
        return result;
    }

    $("#btnBack").click(function () {
        location.href = "/parkingAreas/lifts?parkingId=@Model.ParkingId";
    });
</script>
